cmake_minimum_required(VERSION 3.0.2)
project(yolo_detect)

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++17")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")

option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
add_definitions(-DAPI_EXPORTS)

# TODO(Call for PR): make cmake compatible with Windows
set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
enable_language(CUDA)

set(cv_bridge_DIR /usr/local/share/cv_bridge/cmake) 

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  geometry_msgs
  message_generation  
  cv_bridge
  image_transport
  rospy
  sensor_msgs
  traj_utils
)

add_message_files(
  FILES
  YoloResult.msg
  )

generate_messages(
  DEPENDENCIES
  std_msgs
  geometry_msgs
)
  
catkin_package(
  CATKIN_DEPENDS message_runtime
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
# cuda
include_directories(/usr/local/cuda/include)
link_directories(/usr/local/cuda/lib64)
# tensorrt
# TODO(Call for PR): make TRT path configurable from command line
# 没用
# include_directories(/home/nvidia/TensorRT-8.2.5.1/include/)
# link_directories(/home/nvidia/TensorRT-8.2.5.1/lib/)

include_directories(${PROJECT_SOURCE_DIR}/include/yolo/)
include_directories(${PROJECT_SOURCE_DIR}/plugin/)
file(GLOB_RECURSE YOLO_SRCS ${PROJECT_SOURCE_DIR}/include/yolo/*.cpp ${PROJECT_SOURCE_DIR}/include/yolo/*.cu)
file(GLOB_RECURSE PLUGIN_SRCS ${PROJECT_SOURCE_DIR}/plugin/*.cu)
file(GLOB_RECURSE TRACKER_SRC ${PROJECT_SOURCE_DIR}/include/tracker/*.hpp)

add_library(myplugins SHARED ${PLUGIN_SRCS})
target_link_libraries(myplugins nvinfer cudart)

find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(yolov5_det src/yolov5_det.cpp ${YOLO_SRCS})
target_link_libraries(yolov5_det nvinfer)
target_link_libraries(yolov5_det cudart)
target_link_libraries(yolov5_det myplugins)
target_link_libraries(yolov5_det ${OpenCV_LIBS})

# add_executable(yolov5_ros src/yolov5_ros.cpp ${YOLO_SRCS})
# test for visulization
add_executable(yolov5_ros src/yolov5_ros.cpp ${YOLO_SRCS} ${TRACKER_SRC})
target_link_libraries(yolov5_ros nvinfer)
target_link_libraries(yolov5_ros cudart)
target_link_libraries(yolov5_ros myplugins)
target_link_libraries(yolov5_ros ${OpenCV_LIBS})
target_link_libraries(yolov5_ros ${catkin_LIBRARIES})
add_dependencies(yolov5_ros ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

add_executable(tracker src/tracker.cpp ${TRACKER_SRC})
target_link_libraries(tracker ${catkin_LIBRARIES})
add_dependencies(tracker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
